/*
 *  您可以更改此项目但请不要删除作者署名谢谢，否则根据中华人民共和国版权法进行处理.
 *  You may change this item but please do not remove the author's signature,
 *  otherwise it will be dealt with according to the Copyright Law of the People's Republic of China.
 *
 *  如果需要联系到我请加Wechat: yangbuyiya 备注交流学习
 *  If you need to contact me, please add Wechat: yangbuyiya as a note to exchange and learn.
 *  yangbuyi Copyright (c) https://yby6.com 2024.
 */

package com.yby6.utils;

import com.tencentcloudapi.asr.v20190614.AsrClient;
import com.tencentcloudapi.asr.v20190614.models.CreateRecTaskRequest;
import com.tencentcloudapi.asr.v20190614.models.CreateRecTaskResponse;
import com.tencentcloudapi.asr.v20190614.models.DescribeTaskStatusRequest;
import com.tencentcloudapi.asr.v20190614.models.DescribeTaskStatusResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.yby6.config.TencentConfig;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;

/**
 * 腾讯云语音合成
 *
 * @author Yang Shuai
 * Create By 2023/12/21
 */
@Slf4j
@Component
public class TencentUtils {
    private static final TencentConfig instance = TencentConfig.getInstance();
    
    
    public static DescribeTaskStatusResponse voiceToText(String base64) throws TencentCloudSDKException {
        Credential cred = new Credential(instance.getSecretId(), instance.getSecretKey());
        
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("asr.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        AsrClient client = new AsrClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        CreateRecTaskRequest req = new CreateRecTaskRequest();
        req.setEngineModelType("16k_zh");
        req.setChannelNum(1L);
        req.setResTextFormat(0L);
        req.setSourceType(1L);
        req.setData(base64);
        // 返回的resp是一个CreateRecTaskResponse的实例，与请求对象对应
        CreateRecTaskResponse resp = client.CreateRecTask(req);
        
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeTaskStatusRequest query = new DescribeTaskStatusRequest();
        query.setTaskId(resp.getData().getTaskId());
        // 返回的resp是一个DescribeTaskStatusResponse的实例，与请求对象对应
        
        // 开启短暂轮询查询是否转换完成, 每1s 调用 queryTaskStatus
        return shortPolling(resp.getData().getTaskId());
    }
    
    
    /**
     * 查询任务状态
     */
    public static DescribeTaskStatusResponse queryTaskStatus(Long taskId) throws TencentCloudSDKException {
        Credential cred = new Credential(instance.getSecretId(), instance.getSecretKey());
        
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("asr.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        AsrClient client = new AsrClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeTaskStatusRequest query = new DescribeTaskStatusRequest();
        query.setTaskId(taskId);
        // 返回的resp是一个DescribeTaskStatusResponse的实例，与请求对象对应
        return client.DescribeTaskStatus(query);
    }
    
    /**
     * D短轮询查询是否转换完成
     */
    public static DescribeTaskStatusResponse shortPolling(Long taskId) {
        try {
            DescribeTaskStatusResponse describeTaskStatusResponse = queryTaskStatus(taskId);
            if (describeTaskStatusResponse.getData().getStatusStr().equals("success")) {
                return describeTaskStatusResponse;
            } else {
                Thread.sleep(1000);
                return shortPolling(taskId);
            }
        } catch (Exception e) {
            log.error("【腾讯云语音转文字接口调用异常】", e);
            return null;
        }
    }
    
}
